from flask import Flask, request, send_file
from flask import render_template, request, send_file
import subprocess
import io

app = Flask(__name__)

@app.route("/")
def index():
    context={}
    return render_template("index.html",context=context)

@app.route("/extract_audio", methods=["POST"])
def extract_audio():
    video_file = request.files.get("video")
    if not video_file:
        return {"error": "No video uploaded"}, 400

    video_bytes = video_file.read()
    out_io = io.BytesIO()

    # 用 ffmpeg 从 stdin 转 wav 到 stdout
    process = subprocess.run(
        ["ffmpeg", "-i", "pipe:0", "-vn", "-f", "wav", "pipe:1"],
        input=video_bytes,
        stdout=subprocess.PIPE,
        stderr=subprocess.DEVNULL
    )

    out_io.write(process.stdout)
    out_io.seek(0)

    return send_file(out_io, as_attachment=True, download_name="output.wav", mimetype="audio/wav")

if __name__ == "__main__":
    app.run(debug=True)
